﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Threading;
using System.Globalization;
using System.Data.SqlClient;
using Nivdal.AuxCode;

namespace Nivdal
{
    public partial class OpponentAnalyze : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Title = Resources.Resource.Default_PageTitle + ", " + Resources.Resource.OpponentAnalyze_PageTitle;
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            OnlineUser ou = (OnlineUser)Session["User"];

            if (ou.TeamId == null)
                Response.Redirect("~/Countries.aspx");

            if (!ou.IsPremium)
                Response.Redirect("~/TeamHomePage.aspx");

            if (!IsPostBack)
            {
                if (Request.QueryString["TeamId"] != null)
                {
                    string TeamId = Request.QueryString["TeamId"];
                    Team2HiddenField.Value = TeamId;
                    LanguageHiddenField.Value = ou.Language;
                    TeamHomePageHyperlink.NavigateUrl = "~/TeamHomePage.aspx?TeamId=" + TeamId;
                    TeamHomePageHyperlink.Text = GND.ReadNivdalStringData("TeamId", "Nivdal_GetTeamNameByTeamId", TeamId);
                    HeadToHeadDataSource.SelectParameters.Add("Team1", ou.TeamId);
                    HeadToHeadDataSource.SelectParameters.Add("Team2", Team2HiddenField.Value);
                }
                else
                    Response.Redirect("~/TeamHomePage.aspx");
            }

            if (Team2HiddenField.Value != ou.TeamId)
            {
                LoadAvgGraph(ou.TeamId, Team2HiddenField.Value);
                LoadMaximumsGraph(ou.TeamId, Team2HiddenField.Value);
            }
            else
            {
                ChartsUpdatePanel.Visible = false;
                HeadToHeadPanel.Visible = false;
            }

        }

        protected override void InitializeCulture()
        {
            OnlineUser ou = (OnlineUser)Session["User"];
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(ou.UICulture);
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ou.Culture);
        }

        protected void RefreshPlayersGrid(object sender, EventArgs e)
        {
            BestPlayerGrid.DataBind();
        }

        private void LoadMaximumsGraph(string MyTeam, string HisTeam)
        { 
            string ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            using (SqlConnection conn = new SqlConnection(ConnectionString))
            {
                SqlCommand cmd = new SqlCommand("Nivdal_GetMaxRatingsFor2Teams", conn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.AddWithValue("MyTeam", MyTeam);
                cmd.Parameters.AddWithValue("HisTeam", HisTeam);

                SqlDataReader reader;

                try
                {
                    conn.Open();
                    reader = cmd.ExecuteReader();
                    int index = 0;
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            ComparisonMaxChart.Series[index].Points.AddXY(1, (double)reader.GetDecimal(0));
                            ComparisonMaxChart.Series[index].Points.AddXY(2, (double)reader.GetDecimal(1));
                            ComparisonMaxChart.Series[index].Points.AddXY(3, (double)reader.GetDecimal(2));
                            ComparisonMaxChart.Series[index].Points.AddXY(4, (double)reader.GetDecimal(3));

                            index++;
                        }
                    }

                }
                catch { }
                finally
                {
                    conn.Close();
                }

            }
        }

        
        
        private void LoadAvgGraph(string MyTeam, string HisTeam)
        {
            string ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            using (SqlConnection conn = new SqlConnection(ConnectionString))
            {
                SqlCommand cmd = new SqlCommand("Nivdal_GetAvgRatingsFor2Teams", conn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.AddWithValue("MyTeam", MyTeam);
                cmd.Parameters.AddWithValue("HisTeam", HisTeam);
                cmd.Parameters.AddWithValue("League", LeagueCheckBox.Checked ? "1" : "0");
                cmd.Parameters.AddWithValue("Cup", CupCheckBox.Checked ? "1" : "0");
                cmd.Parameters.AddWithValue("IntCup", InternationalCupCheckBox.Checked ? "1" : "0");

                SqlDataReader reader;

                try
                {
                    conn.Open();
                    reader = cmd.ExecuteReader();
                    int index = 0;
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            ComparisonAvgChart.Series[index].Points.AddXY(1, (double)reader.GetDecimal(0));
                            ComparisonAvgChart.Series[index].Points.AddXY(2, (double)reader.GetDecimal(1));
                            ComparisonAvgChart.Series[index].Points.AddXY(3, (double)reader.GetDecimal(2));
                            ComparisonAvgChart.Series[index].Points.AddXY(4, (double)reader.GetDecimal(3));
                            ComparisonAvgChart.Series[index].Points.AddXY(5, (double)reader.GetDecimal(4));

                            index++;
                        }
                    }

                }
                catch { }
                finally
                {
                    conn.Close();
                }

            }
        }

        protected void ReloadAvgGraph(object sender, EventArgs e)
        { 
            
        }

        protected string InjuryIconName(object Injury)
        {
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            return GND.InjuryIconName(Injury);
        }
    }
}